
#include "client.h"

int main(int argc, char *argv[])
{
    int cnd = tcp_cnd_init(argv[1], argv[2]);
    if (-1 == cnd)
    {
        perror("tcp_cnd_init");
        puts("离线模式");
    }
    else
    {
        puts("服务器连接成功");
        int ack_clients = 0;
        char cmd_user = 0;
        fd_set r_set, b_set;
        FD_ZERO(&b_set);
        FD_SET(STDIN_FILENO, &b_set);
        FD_SET(cnd, &b_set);
        int fds = cnd + 1;
        char buf_send[1024];
        char buf_recv[1024];
        send(cnd, GET_CLIENT, 27, 0);
        recv(cnd,buf_recv, 1024, 0);
        if (0 == strncmp(buf_recv,ACK,14))
        {
            char *ack = buf_recv + 14;
            ack_clients = atoi(ack);
        }
        while (1)
        {
            r_set = b_set;
            struct timeval tm;
            tm.tv_sec = 5;
            tm.tv_usec = 5000;
            int ret = select(fds, &r_set, NULL, NULL, &tm);
            ERROR_CHECK(-1, ret, "select");

            if (FD_ISSET(STDIN_FILENO, &r_set))
            {
                scanf("%d",&cmd_user);
                getchar();
                switch (cmd_user)
                {
                case 1:
                    user_log();
                    break;
                case 2:
                    user_reg();
                    break;
                case 3:
                    user_del();
                    break;
                case 4:
                    user_alt();
                    break;
                default:
                    puts("退出");
                    return 0;
                    break;
                }
            }
            if (FD_ISSET(cnd, &r_set))
            {
            }
            system("clear");
            printf("****************************************\n");
            printf("********欢迎使用分布式任务处理系统********\n");
            printf("*1、登录 2、注册 3、注销 4、修改 任意键退出 *\n");
            printf("****************************************\n");
        }
    }
    return 0;
}
